package com.github.chirspan.xaas.uc.web.api;

import com.github.chirspan.xaas.api.BaseCRUDRestController;
import com.github.chirspan.xaas.core.rest.RestResult;
import com.github.chirspan.xaas.log.annotation.ApiLog;
import com.github.chirspan.xaas.rbac.model.RbacRole;
import com.github.chirspan.xaas.uc.service.IRbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * ************************
 * <p>角色管理API</p>
 * *************************
 *
 * @author chenpan
 * @date 2021/2/03 14:08
 */
@RestController
@RequestMapping("role")
public class RbacRoleController extends BaseCRUDRestController<RbacRole> {

    @Autowired
    private IRbacRoleService roleService;

    @PutMapping("/{roleId}/resource")
    @ApiLog("更新角色权限")
    public RestResult setResource(@PathVariable("roleId") String roleId,
                                  @RequestParam(value = "resourceIds[]", required = false) String[] resourceIds) {
        roleService.updateRoleResource(roleId, resourceIds);
        return RestResult.OK(roleId, "角色权限更新成功");
    }

    @GetMapping("/{roleId}/resource")
    public RestResult getRoleResource(@PathVariable("roleId") String roleId){
        return RestResult.OK(roleService.getRoleResourcesById(roleId));
    }

    @GetMapping("/roleId")
    public RestResult getRoleIdsByRole(Set<String> roles){
        return RestResult.OK(roleService.getRoleIdsByRole(roles));

    }
}
